<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/03/28 11:12:22 $)
// $RCSfile: index.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

define("SWIFT_AREA", 10);

if (defined("SWIFTCUSTOMPATH"))
{
	chdir(SWIFTCUSTOMPATH);
} else {
	chdir('./../');
}
require_once ("./swift.php");

// As the entire API Revolves around XML, We load the XML Functions right from start
require_once ("./includes/functions_xml.php");
require_once ("./includes/functions_api.php");
$xml = new XML;

if ($_POST["_ca"] == "login") {
	$staffid = $staffauth->authenticate($_POST["username"], $_POST["password"], false, false);
	if (!empty($staffid) && in_array($staffid, $_SWIFT["settings"]["t_apistaffusers"]))
	{
		$session->insertSession(SESSION_API, $staffid);

		$xml->addParentTag("api");
		$xml->addTag("status", "1");
			$xml->addParentTag("data");
				$xml->addTag("sessionid", $_SWIFT["session"]["sessionid"]);
			$xml->endTag("data");
		$xml->endTag("api");
		$xml->echoXML();
	} else {
		// Authentication Failed
		$xml->addTag("api");
			$xml->addTag("status", "0");
			$xml->addTag("errorno", ERROR_LOGINFAILURE);
			$xml->addTag("errortxt", $_SWIFT["language"]["invaliduser"]);
		$xml->endTag("api");
		$xml->echoXML();
	}
} else if ($_POST["_ca"] == "logout") {
	$session->logoutSession(SESSION_API);

	$xml->addParentTag("api");
		$xml->addTag("status", "1");
			$xml->addParentTag("data");
				$xml->addTag("message", $_SWIFT["language"]["loggedout"]);
			$xml->endTag("data");
	$xml->endTag("api");
	$xml->echoXML();
} else {
	if (!$session->start(SESSION_API)) {
		// Session expired
		$xml->addParentTag("api");
			$xml->addTag("status", "0");
			$xml->addTag("errorno", ERROR_INVALIDSESSION);
			$xml->addTag("errortxt", $_SWIFT["language"]["invalid_sessionid"]);
		$xml->endTag("api");
		$xml->echoXML();
		exit;
	}

	$events->triggerEvent(EVENT_API);
}
?>